#include <stdio.h>
#include <stdlib.h>

typedef struct _sum_list
{
    int n ;
    int sum;
    struct _sum_list *next;
}sum_list;

sum_list * search(sum_list *head, int n)
{
    while(head)
    {
        if(head->n == n)
        {
            break;
        }
        head = head->next;
    }

    return head;
}


int main()
{
    int sum  = 0;
    int n = 0;
    
    sum_list *head = NULL;
    sum_list *node = NULL;

    while(1)
    {
         int i = 1;
         sum = 0;
         node = NULL;

         printf("请输入n值：\n");
         if(!scanf("%d", &n))
         {
             break;
         }

         node = search(head,n);
         if(node)
         {
            sum = node->sum;
            printf("query the sum!\n");
         }
         else
        {
            for(;i<=n;i++)
            {
                sum += i;
            }
            node = malloc(sizeof(sum_list));
            node->n = n;
            node->sum = sum;

            sum_list *tmp = NULL;
            tmp = head;
            head = node;
            
            printf("calc the sum!\n");
        }


         printf("sum=%d\n",sum);
    }



    return 0;
}

